<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-select placeholder="请选择公司" v-model="list.query.company_id">
						<el-option v-for="item in company" :label="item.company_name" :key="item.id" :value="item.id"></el-option>
					</el-select>

					<el-select placeholder="请选择品牌" v-model="list.query.company_sign_id">
						<el-option v-for="item in sign" :label="item.company_sign" :key="item.id" :value="item.id"></el-option>
					</el-select>

					<el-input placeholder="货号" v-model="list.query.goods_number" style="width: 120px"/>
					<el-input placeholder="品名" v-model="list.query.goods_desc" style="width: 220px"/>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>

		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" @dataChange="tableLoad" row-key="id" @selection-change="selectionChange" stripe  style="font-size: 15px;">
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="产品id" prop="id" width="80"></el-table-column>
				<el-table-column label="货号" prop="goods_numner" width="200">
					<template #default="scope">
						<el-tag effect="dark" style="font-weight: bold; font-size: 20px; padding: 15px;">{{scope.row.goods_number}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="客户信息" prop="email" width="250">
					<template #default="scope">
						{{scope.row.company_name}} / {{scope.row.company_sign}}
					</template>
				</el-table-column>
				<el-table-column label="品名" prop="goods_desc" width="200"></el-table-column>
				<el-table-column label="一箱数量" prop="num" >
					<template #default="scope">
						<el-button type="primary" icon="el-icon-edit" @click="table_edit(scope.row)">{{scope.row.num}}</el-button>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="right">
					<template #default="scope">
						<el-button plain size="small" @click="table_show(scope.row)">编辑</el-button>
						<el-button plain size="small" @click="price_code(scope.row)">绑定打印码</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" width="600" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

	<el-drawer v-model="dialog.info" ref="infodrawer"  :size="900" title="详细" direction="rtl" custom-class="demo-drawer" destroy-on-close>
		<el-container style="">
			<info ref="infoDialog" @success="handleSuccess"></info>
		</el-container>
	</el-drawer>

</template>

<script>
	import saveDialog from './save'
	import info from './info'

	export default {
		name: 'listCrud',
		components: {
			saveDialog,
			info
		},
		data() {
			return {
				dialog:{
					save: false,
					info: false
				},
				list: {
					apiObj: this.$API.goods.list,
					query: {
						company_id: "",
						company_sign_id: "",
						goods_number: "",
						goods_desc: ""
					}
				},
				// 查询条件
				company: [],
				sign: [],
				selection: []
			}
		},
		mounted() {

		},
		methods: {
			tableLoad(res) {
				this.company = res.company
				this.sign = res.sign
			},
			upsearch(page = 1) {
				this.$refs.table.upData(this.list.query, page)		
			},
			//窗口新增
			add(){
				this.dialog.info = true
				this.$nextTick(() => {
					this.$refs.infoDialog.setData('add', {}, this.company, this.sign)
				})
			},
			//窗口编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//页面新增
			addPage(){
				this.$router.push({
					path: '/goods/detail',
				})
			},
			//页面编辑
			table_edit_page(row){
				this.$router.push({
					path: '/template/list/crud/detail',
					query: {
						id: row.id
					}
				})
			},
			//查看
			table_show(row){
				this.dialog.info = true
				var company = this.company
				var sign = this.sign
				this.$nextTick(() => {
					this.$refs.infoDialog.setData('edit', row, company, sign)
				})
			},
			//删除明细
			async table_del(row, index){
				var reqData = {id: row.id}
				var res = await this.$API.demo.post.post(reqData);
				if(res.code == 200){
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if(!confirmRes){
					return false
				}

				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				this.$message.success("操作成功")

			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				//为了减少网络请求，直接变更表格内存数据
				if(mode=='add'){
					this.$refs.table.unshiftRow(data)
				}else if(mode=='edit'){
					this.$refs.table.updateKey(data)
				}

				//当然也可以暴力的直接刷新表格
				// this.$refs.table.refresh()
			},
			handleSuccess() {
				this.$refs.table.refresh()
			},
			async price_code(row) {
				var reqData = {
					goods_number: row.goods_number,
					sign: row.company_sign
				}
				var res = await this.$API.goods.priceCode.get(reqData)
				if (res.code == 200) {
					this.$message.success("绑定成功")
				} else {
					this.$message.success("绑定失败")
				}
			}
		}
	}
</script>

<style>
</style>
